<template>
  <div>
    <el-form label-width="70px">
      <el-form-item label="SKU名称">
        <el-input
          placeholder="SKU名称"
          size="normal"
          clearable
          @change=""
          v-model="skuParams.skuName"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          size="normal"
          clearable
          @change=""
          type="number"
          v-model="skuParams.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(克)">
        <el-input
          placeholder="重量(克)"
          size="normal"
          clearable
          @change=""
          type="number"
          v-model="skuParams.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="SKU描述">
        <el-input
          placeholder="SKU描述"
          size="normal"
          clearable
          @change=""
          type="textarea"
          v-model="skuParams.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item
            v-for="(item) in attrArr"
            :key="item.id"
            :label="item.attrName"
          >
            <el-select v-model="item.attrIdAndValueId">
              <el-option
                v-for="(attrValue) in item.attrValueList"
                :key="attrValue.id"
                :label="attrValue.valueName"
                :value="`${item.id}:${attrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item
            v-for="(item) in saleArr"
            :key="item.id"
            :label="item.saleAttrName"
          >
            <el-select v-model="item.saleIdAndValueId">
              <el-option
                :value="`${item.id}:${saleAttrValue.id}`"
                v-for="(saleAttrValue) in item.spuSaleAttrValueList"
                :key="saleAttrValue.id"
                :label="saleAttrValue.saleAttrValueName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片名称">
        <el-table border :data="imgArr" ref="table">
          <el-table-column type="selection" width="80px" align="center"></el-table-column>
          <el-table-column label="图片">
            <template #="{ row}">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template #="{row}">
              <el-button type="primary" size="default" @click="handler(row)"
                >设置默认</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="save">保存</el-button>
        <el-button type="primary" size="default" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
//引入请求API
import { reqAttr } from "@/api/product/attr";
import { reqSpuImageList, reqSpuHasSaleAttr, addSaveSku } from "@/api/product/spu";
import { SkuData } from "@/api/product/spu/type";
import { ElMessage } from "element-plus";
import { reactive, ref, } from "vue";
//收集sku参数
let skuParams = reactive<SkuData>({
  category3Id: "", //三级分类的ID
  price: '', //SKU价格
  skuAttrValueList: [], //平台属性收级
  skuDefaultImg: "", //售卖商品图片地址
  skuDesc: "", //sku描述
  skuName: "", //新增SKU名称
  skuSaleAttrValueList: [], //销售属性
  spuId: "", //已有SPUID
  tmId: "", //品牌ID
  weight: '', //SKU重量
});

//获取table组件实例
let table = ref<any>();

let attrArr = ref<any>([]);

//所有销售属性
let saleArr = ref<any>([]);

//照片墙数据
let imgArr = ref<any>([]);

//获取父组件传递自定义事件
let $emit = defineEmits(["changeScene"]);
const cancel = () => {
  $emit("changeScene", { flag: 0, params: "" });
};
//当子组件对外暴露，父组件获取
const initSkuDate = async (c1Id: number | string, c2Id: number | string, spu: any) => {
  //获取平台属性
  console.log(c1Id, c2Id, spu);
  let result: any = await reqAttr(c1Id, c2Id, spu.category3Id);
  //获取对应销售属性
  let result1: any = await reqSpuHasSaleAttr(spu.id);
  //获取照片墙的数据
  let result2: any = await reqSpuImageList(spu.id);
  attrArr.value = result.data;
  saleArr.value = result1.data;
  imgArr.value = result2.data;
  //收集数据
  skuParams.category3Id = spu.category3Id;
  skuParams.spuId = spu.id;
  skuParams.tmId = spu.tmId;
};
//对外暴露方法
defineExpose({
  initSkuDate,
});
//设置图片默认值
const handler = (row: any) => {
  //派他思想
  //点击的时候，全部的复选框不勾选
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false);
  });
  //选中状态设置
  table.value.toggleRowSelection(row, true);

  //收集图片地址
  skuParams.skuDefaultImg = row.imgUrl;
};
//保存按钮方法
const save = async () => {
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId,valueId] = next.attrIdAndValueId.split(":");
      prev.push({
        attrId,
        valueId
      });
    }
    return prev;
  }, []);
  //销售属性
  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(":");
      prev.push({
        saleAttrId,
        saleAttrValueId,
      });
    }
    return prev;
  }, []);
  skuParams.price = Number(skuParams.price)
  skuParams.weight = Number(skuParams.weight)
  let result = await addSaveSku(skuParams);
  console.log(result)
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "添加SKU成功",
    });
    $emit('changeScene',{flag:0,params:''})
  } else {
    ElMessage({
      type: "error",
      message: "添加SKU失败",
    });
  }
};

</script>

<style scoped>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
  margin-bottom: 10px;
}
</style>
